<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>成绩统计</title>
</head>
  </style>
  <body>
    <div id="app" class="score-case">
      <div class="table">
        <h2>成绩统计</h2>
        <table>
          <thead>
            <tr>
              <th>编号</th>
              <th>科目</th>
              <th>成绩</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="(item,index) in list" :key="item.id">
              <td>{{index+1}}</td>
              <td>{{item.subject}}</td>
              <td :class="{red:item.score<60}">{{item.score}}</td>
              <td @click.prevent="del(item.id)">删除</td>
            </tr>
          </tbody>
          <tfoot>
            <tr>
              <td colspan="5">
                <span>总分：{{total}}</span>
                <span style="margin-left: 50px">平均分：{{average}}</span>
              </td>
            </tr>2
          </tfoot>
        </table>
      </div>
      <div class="form">
        <div class="form-item">
          <div class="label">科目：</div>
          <div class="input">
            <input
              type="text"
              v-model.trim="subject"
              placeholder="请输入科目"
            />
          </div>
        </div>
        <div class="form-item">
          <div class="label">分数：</div>
          <div class="input">
            <input
              type="text"
              v-model.number="score"
              placeholder="请输入分数"
            />
          </div>
        </div>
        <div class="form-item">
          <div class="label"></div>
          <div class="input">
            <button @click="add" class="submit" >添加</button>
          </div>
        </div>
      </div>
    </div>
    <script src="./js/2.7.14_dist_vue.js"></script>
    <script>
      const app = new Vue({
        el: '#app',
        data: {
          list: [
            { id: 1, subject: '语文', score: 20 },
            { id: 7, subject: '数学', score: 99 },
            { id: 12, subject: '英语', score: 70 },
          ],
          subject: '',
          score: 0
        },
       computed: {
         total(){        
          return (this.list.reduce((prev,item)=>prev+item.score,0))
         },
         average(){
          if(this.list.length===0){
            return 0
          }
          return (this.total/this.list.length).toFixed(2)
         }
       },
        methods: {
          del(id){ 
          this.list=this.list.filter(item=>item.id!==id)
          },
          add(){
            if(this.subject.length===0 || typeof this.score !== 'number'){
                return alert('请确认您输入的是正确的内容')
            }
            this.list.unshift(
              {id:+new Date(),
               subject:this.subject,
               score:this.score
              }
            )
            this.subject=''
            this.score=0
          }
        }
 
      })
    </script>
  </body>
</html>